import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-10-05
 * Time: 9:52
 */
//https://leetcode.cn/problems/contains-duplicate-ii/submissions/570121301/
//存在重复元素
public class Solution4 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                int abs=Math.abs(map.get(nums[i])-i);
                if(abs<=k){
                    return true;
                }
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
